/**
 * 
 */
package com.cwnuzj.ffms.util;

import java.util.Arrays;
import java.util.Iterator;

import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;

/**
 * RCP操作工具类，如打开，关闭视图、编辑器等
 * 
 * @author jeff.zheng
 * 
 */
public class RCPUtil {

	/**
	 * 激活或打开相应的编辑器
	 * 
	 * @param editorId
	 */
	public static void openEditor(IHandlerService handlerService, String editorId, String commandId) {
		IEditorReference[] editorReferences = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
		Iterator<IEditorReference> iterator = Arrays.asList(editorReferences).iterator();
		boolean ifFind = false;
		while (iterator.hasNext()) {
			IEditorReference editor = iterator.next();
			if (editorId.equalsIgnoreCase(editor.getId())) {
				// 如果已经打开，则激活对应的编辑器
				editor.getPage().activate(editor.getPart(true));
				ifFind = true;
				break;
			}
		}
		if (!ifFind) {
			// 否则打开对应的编辑器
			try {
				handlerService.executeCommand(commandId, null);
			} catch (Exception ex) {
				System.out.println(ex);
			}
		}

	}

}
